
Partial Class Correo
    Inherits System.Web.UI.Page

    Private strIP As String
    Private strUsrMail As String
    Private strPwdMail As String
    Private strPara As String

    Protected m_szConexion As String = ConfigurationManager.AppSettings("Conexion")

    Protected Sub LeerParametrosCorreo()
        Using exec As DotNetDAL.SQLPersistence = New DotNetDAL.SQLPersistence(True, m_szConexion)
            Using rp As DotNetResponse.SQLPersistence = New DotNetResponse.SQLPersistence
                exec.ExecuteDataTable(rp, "SELECT * FROM CONF_CORREO")

                If rp.TieneDatos Then
                    strIP = rp.DtTable.Rows(0)(0)
                    strUsrMail = rp.DtTable.Rows(0)(1)
                    strPwdMail = rp.DtTable.Rows(0)(2)
                    strPara = rp.DtTable.Rows(0)(3)
                End If
            End Using
            exec.Dispose()
        End Using
    End Sub

    Protected Sub ValidarIP(ByVal source As System.Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles CustomValidator3.ServerValidate
        args.IsValid = (Utils.ValidaIP(args.Value.Trim))
    End Sub

    Protected Sub ActualizarConfCorreo(ByVal Servidor As String, ByVal CorreoDE As String, ByVal ClaveCorreoDE As String, ByVal CorreoPara As String)
        Using exec As DotNetDAL.SQLPersistence = New DotNetDAL.SQLPersistence(True, m_szConexion)
            Using rp As DotNetResponse.SQLPersistence = New DotNetResponse.SQLPersistence
                Dim sSQL As String = "UPDATE CONF_CORREO SET SERVIDOR = '" & Servidor & "', CORREO_DE = '" & CorreoDE & "', CLAVE_CORREO_DE = '" & ClaveCorreoDE & "', CORREO_PARA = '" & CorreoPara & "' WHERE ID = 1"

                rp.StoredProcedure = sSQL
                exec.ExecuteNonQuery(rp, sSQL)
                exec.Commit()
            End Using
            exec.Dispose()
        End Using
    End Sub

    Protected Sub btnGuardarConfCorreo_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGuardarConfCorreo.Click
        ActualizarConfCorreo(Me.txtServidorCorreo.Text.Trim, Me.txtCorreoDE.Text.Trim, Me.txtClaveCorreoDE.Text.Trim, Me.txtPara.Text.Trim)
    End Sub

    Protected Sub btnLimpiarConfCorreo_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLimpiarConfCorreo.Click
        txtServidorCorreo.Text = ""
        txtCorreoDE.Text = ""
        txtClaveCorreoDE.Attributes.Add("value", "")
        txtPara.Text = ""
    End Sub

    Protected Sub ValidarCorreo(ByVal source As System.Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles CustomValidator1.ServerValidate
        args.IsValid = EmailAddressCheck(args.Value.Trim)
    End Sub

    Protected Sub ValidarCorreos(ByVal source As System.Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles CustomValidator2.ServerValidate
        Dim Correos() As String = args.Value.Trim.Split(",")

        For Each Correo As String In Correos
            args.IsValid = EmailAddressCheck(Correo.Trim)

            If Not args.IsValid Then
                Exit For
            End If
        Next
    End Sub

    Function EmailAddressCheck(ByVal emailAddress As String) As Boolean
        Dim pattern As String = "^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$"
        Dim emailAddressMatch As Match = Regex.Match(emailAddress, pattern)
        If emailAddressMatch.Success Then
            EmailAddressCheck = True
        Else
            EmailAddressCheck = False
        End If
    End Function

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            LeerParametrosCorreo()

            txtServidorCorreo.Text = Me.strIP
            txtCorreoDE.Text = Me.strUsrMail

            txtClaveCorreoDE.Attributes.Add("value", Me.strPwdMail)
            'txtClaveCorreoDE.Text = "***********" 'Me.strPwdMail
            txtPara.Text = Me.strPara
        End If
    End Sub
End Class
